package com.hospital.ecard.patienthandler;

import com.hospital.common.core.module.controller.vo.PatientReqEntity;
import com.hospital.common.core.utils.SpringContextHolder;
import com.hospital.ecard.module.entity.PatientInfo;
import com.hospital.ecard.module.service.PatientInfoService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author lvyao
 * @description 绑定就诊卡或者院内ID
 * @date 2021/5/6
 */
@Slf4j
public class PatientInfoBindHandler implements PatientInfoElectronicHandler {

    private PatientInfoService patientInfoService = SpringContextHolder.getBean(PatientInfoService.class);

    @Override
    public PatientInfo patientHandleInfo(PatientReqEntity patientReqEntity) {
        PatientInfo patientInfo = new PatientInfo();
        String hosCardNo = patientReqEntity.getHosCardNo();
        List<PatientInfo> patientInfoList = patientInfoService.selectPatientInfoByIdNo(hosCardNo);
        if (patientInfoList == null || patientInfoList.isEmpty()) {
            throw new RuntimeException("根据患者ID或实体卡号获取不到信息，请前往窗口核实信息！");
        } else if (patientInfoList.size() > 1) {
            throw new RuntimeException("根据患者ID或实体卡号获取多条信息，请前往窗口核实信息！");
        } else {
            patientInfo = patientInfoList.get(0);
            if (patientInfo.getIdNo() == null || patientInfo.getIdNo().equals("")){
                String errormsg = "根据患者ID或实体卡号获取患者无身份证号，请前往窗口核实并补充身份证信息！";
                throw new RuntimeException(errormsg);
            }else if ( !patientReqEntity.getIdNo().toUpperCase().equals(patientInfo.getIdNo().toUpperCase()) ){
                String errormsg = "根据患者ID或实体卡号获取患者身份证号为{" + patientInfo.getIdNo() + "}与填写身份证{" + patientReqEntity.getIdNo() + "}不符，请前往窗口核实信息！";
                throw new RuntimeException(errormsg);
            }
            patientInfo.setIdNo(patientReqEntity.getIdNo().toUpperCase());
            boolean isCheck = patientReqEntity.getCreateChannel()!=null&&patientReqEntity.getCreateChannel().equals("1")?true:false;
            if (!isCheck && !patientReqEntity.getPatientName().trim().equals(patientInfo.getName().trim())) {
                String errormsg = "根据患者ID或实体卡号获取患者姓名为{" + patientInfo.getName() + "}与填写姓名{" + patientReqEntity.getPatientName() + "}不符，请前往窗口核实信息！";
                throw new RuntimeException(errormsg);
            }
        }
        patientInfo.setPhoneNumberHome(patientReqEntity.getPhone());
        patientReqEntity.setDataOper(0);
        return patientInfo;
    }
}

